package com.example.mobifin_busticket.models;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;

public class BusModel {
	private String Bus_id, ACValue, arrivalTime, busType, busTypeId,
			cancellationPolicy, departureTime, doj, idProofRequired,
			liveTrackingAvailable, nonAC, operator, partialCancellationAllowed,
			routeId, seater, sleeper, travels, mTicketEnabled;
	private ArrayList<Boarding_DropingPointModel> boardingPointList;
	private ArrayList<Boarding_DropingPointModel> dropingPointList;
	private ArrayList<FareDetailsModel> faredetailsList;
	private ArrayList<FareModel> fareList;

	private int availableSeats;
	

	

	public String getBus_id() {
		return Bus_id;
	}

	public void setBus_id(String Bus_id) {
		this.Bus_id = Bus_id;
	}

	public String getACValue() {
		return ACValue;
	}

	public void setACValue(String ACValue) {
		this.ACValue = ACValue;
	}

	public String getarrivalTime() {

		return arrivalTime;
	}

	public void setarrivalTime(String arrivalTime) {
		this.arrivalTime = arrivalTime;
	}

	public int getavailableSeats() {
		return availableSeats;
	}

	public void setavailableSeats(int availableSeats) {
		this.availableSeats = availableSeats;
	}

	public String getbusType() {
		return busType;
	}

	public void setbusType(String busType) {
		this.busType = busType;
	}

	public String getbusTypeId() {
		return busTypeId;
	}

	public void setbusTypeId(String busTypeId) {
		this.busTypeId = busTypeId;
	}

	public String getcancellationPolicy() {
		return cancellationPolicy;
	}

	public void setcancellationPolicy(String cancellationPolicy) {
		this.cancellationPolicy = cancellationPolicy;
	}

	public String getdepartureTime() {
		return departureTime;
	}

	public void setdepartureTime(String departureTime) {
		this.departureTime = departureTime;
	}

	public String getdoj() {
		return doj;
	}

	public void setdoj(String doj) {
		this.doj = doj;
	}

	public String getidProofRequired() {
		return idProofRequired;
	}

	public void setidProofRequired(String idProofRequired) {
		this.idProofRequired = idProofRequired;
	}

	public String getliveTrackingAvailable() {
		return liveTrackingAvailable;
	}

	public void setliveTrackingAvailable(String liveTrackingAvailable) {
		this.liveTrackingAvailable = liveTrackingAvailable;
	}

	public String getnonAC() {
		return nonAC;
	}

	public void setnonAC(String nonAC) {
		this.nonAC = nonAC;
	}

	public String getoperator() {
		return operator;
	}

	public void setoperator(String operator) {
		this.operator = operator;
	}

	public String getpartialCancellationAllowed() {
		return partialCancellationAllowed;
	}

	public void setpartialCancellationAllowed(String partialCancellationAllowed) {
		this.partialCancellationAllowed = partialCancellationAllowed;
	}

	public String getrouteId() {
		return routeId;
	}

	public void setrouteId(String routeId) {
		this.routeId = routeId;
	}

	public String getseater() {
		return seater;
	}

	public void setseater(String seater) {
		this.seater = seater;
	}

	public String getsleeper() {
		return sleeper;
	}

	public void setsleeper(String sleeper) {
		this.sleeper = sleeper;
	}

	public String gettravels() {
		return travels;
	}

	public void settravels(String travels) {
		this.travels = travels;
	}

	public String getmTicketEnabled() {
		return mTicketEnabled;
	}

	public void setmTicketEnabled(String mTicketEnabled) {
		this.mTicketEnabled = mTicketEnabled;
	}

	public ArrayList<Boarding_DropingPointModel> getBoardingPointList() {
		return boardingPointList;
	}

	public void setBoardingPointList(
			ArrayList<Boarding_DropingPointModel> boardingPointList) {
		this.boardingPointList = boardingPointList;
	}

	public ArrayList<Boarding_DropingPointModel> getDropingPointList() {
		return dropingPointList;
	}

	public void setDropingPointList(
			ArrayList<Boarding_DropingPointModel> dropingPointList) {
		this.dropingPointList = dropingPointList;
	}

	public ArrayList<FareDetailsModel> getFaredetailsList() {
		return faredetailsList;
	}

	public void setFaredetailsList(ArrayList<FareDetailsModel> faredetailsList) {
		this.faredetailsList = faredetailsList;
	}

	public ArrayList<FareModel> getFareList() {
		return fareList;
	}

	public void setFareList(ArrayList<FareModel> fareList) {
		this.fareList = fareList;
	}

	public static ArrayList<BusModel> parseTripsDetails(String response) {
		ArrayList<BusModel> busList = new ArrayList<BusModel>();

		try {
			// Parsing logic will goes here

			JSONObject json = new JSONObject(response);
			JSONArray availableTripArray = json.getJSONArray("availableTrips");
			for (int i = 0; i < availableTripArray.length(); i++) {

				JSONObject tripsObj = availableTripArray.getJSONObject(i);

				BusModel tripModel = new BusModel();
				ArrayList<Boarding_DropingPointModel> dropingPoitnlist = new ArrayList<Boarding_DropingPointModel>();

				ArrayList<FareModel> fareList = new ArrayList<FareModel>();

				tripModel.setBus_id(tripsObj.getString("id"));
				tripModel.setACValue(tripsObj.getString("AC"));
				tripModel.setarrivalTime(ConvertTime(tripsObj
						.getLong("arrivalTime")));
				tripModel.setavailableSeats(tripsObj.getInt("availableSeats"));

				// parsing boarding points

				if (tripsObj.optJSONArray("boardingTimes") instanceof JSONArray) {
					JSONArray boardingTimes = tripsObj
							.getJSONArray("boardingTimes");
					ArrayList<Boarding_DropingPointModel> boardingPoitnlist = new ArrayList<Boarding_DropingPointModel>();

					for (int j = 0; j < boardingTimes.length(); j++) {
						JSONObject boardingObj = boardingTimes.getJSONObject(j);

						Boarding_DropingPointModel boardinPointModel = new Boarding_DropingPointModel();
						boardinPointModel.setBusId(tripsObj.getString("id"));
						boardinPointModel.setBoardingPointId(boardingObj
								.getString("bpId"));
						boardinPointModel.setBordingPointName(boardingObj
								.getString("bpName"));
						boardinPointModel.setlocation(boardingObj
								.getString("location"));
						boardinPointModel.setprime(boardingObj
								.getString("prime"));
						boardinPointModel.settime(ConvertTime(boardingObj
								.getLong("time")));
						// dbManager.insertBoardingTimeInfo(Busid, bpId, bpName,
						// location, prime, time);
						boardingPoitnlist.add(boardinPointModel);
						System.out
								.println("boarding point has been set for bus id"
										+ tripModel.getBus_id());
						tripModel.setBoardingPointList(boardingPoitnlist);
						System.out
								.println("------------>>>>>Bording point size:"
										+ tripModel.getBoardingPointList()
												.size());
					}
				} else if (tripsObj.optJSONObject("boardingTimes") instanceof JSONObject) {
					ArrayList<Boarding_DropingPointModel> boardingPoitnlist = new ArrayList<Boarding_DropingPointModel>();

					JSONObject boardingObj = tripsObj
							.getJSONObject("boardingTimes");
					Boarding_DropingPointModel boardinPointModel = new Boarding_DropingPointModel();
					boardinPointModel.setBusId(tripsObj.getString("id"));
					boardinPointModel.setBoardingPointId(boardingObj
							.getString("bpId"));
					boardinPointModel.setBordingPointName(boardingObj
							.getString("bpName"));
					boardinPointModel.setlocation(boardingObj
							.getString("location"));
					boardinPointModel.setprime(boardingObj.getString("prime"));
					boardinPointModel.settime(ConvertTime(boardingObj
							.getLong("time")));
					// dbManager.insertBoardingTimeInfo(Busid, bpId, bpName,
					// location, prime, time);
					boardingPoitnlist.add(boardinPointModel);
					System.out.println("boarding point has been set for bus id"
							+ tripModel.getBus_id());
					tripModel.setBoardingPointList(boardingPoitnlist);
					System.out.println("------------>>>>>Bording point size:"
							+ tripModel.getBoardingPointList().size());

				}
				tripModel.setbusType(tripsObj.getString("busType"));
				tripModel.setbusTypeId(tripsObj.getString("busTypeId"));
				tripModel.setcancellationPolicy(tripsObj
						.getString("cancellationPolicy"));
				tripModel.setdepartureTime(ConvertTime(tripsObj
						.getLong("departureTime")));
				tripModel.setdoj(tripsObj.getString("doj"));

				// Parsing droping details

				if (tripsObj.optJSONArray("droppingTimes") instanceof JSONArray) {
					JSONArray droppingTimes = tripsObj
							.getJSONArray("droppingTimes");

					for (int j = 0; j < droppingTimes.length(); j++) {
						JSONObject dropingObj = droppingTimes.getJSONObject(j);

						Boarding_DropingPointModel dropingPointModel = new Boarding_DropingPointModel();
						dropingPointModel.setBusId(tripsObj.getString("id"));
						dropingPointModel.setBoardingPointId(dropingObj
								.getString("bpId"));
						dropingPointModel.setBordingPointName(dropingObj
								.getString("bpName"));
						dropingPointModel.setlocation(dropingObj
								.getString("location"));
						dropingPointModel.setprime(dropingObj
								.getString("prime"));
						dropingPointModel.settime(ConvertTime(dropingObj
								.getLong("time")));
						// dbManager.insertBoardingTimeInfo(Busid, bpId, bpName,
						// location, prime, time);
						dropingPoitnlist.add(dropingPointModel);
						System.out
								.println("Droping point has been set for bus id"
										+ tripModel.getBus_id());
						tripModel.setDropingPointList(dropingPoitnlist);
						System.out
								.println("------------>>>>>Droping point size:"
										+ tripModel.getDropingPointList()
												.size());

					}
				} else if (tripsObj.optJSONObject("droppingTimes") instanceof JSONObject) {
					JSONObject dropingObj = tripsObj
							.getJSONObject("droppingTimes");
					Boarding_DropingPointModel dropingPointModel = new Boarding_DropingPointModel();

					dropingPointModel.setBusId(tripsObj.getString("id"));
					dropingPointModel.setBoardingPointId(dropingObj
							.getString("bpId"));
					dropingPointModel.setBordingPointName(dropingObj
							.getString("bpName"));
					dropingPointModel.setlocation(dropingObj
							.getString("location"));
					dropingPointModel.setprime(dropingObj.getString("prime"));
					dropingPointModel.settime(ConvertTime(dropingObj
							.getLong("time")));
					// dbManager.insertBoardingTimeInfo(Busid, bpId, bpName,
					// location, prime, time);
					dropingPoitnlist.add(dropingPointModel);
					System.out.println("droping point has been set for bus id"
							+ tripModel.getBus_id());
					tripModel.setDropingPointList(dropingPoitnlist);
					System.out.println("------------>>>>>Droping point size:"
							+ tripModel.getDropingPointList().size());

				} else {
					tripModel.setDropingPointList(dropingPoitnlist);

				}
				// Parsing fare details
				if (tripsObj.optJSONArray("fareDetails") instanceof JSONArray) {
					JSONArray fareDetails = tripsObj
							.getJSONArray("fareDetails");

					for (int j = 0; j < fareDetails.length(); j++) {
						JSONObject faredetailsObj = fareDetails
								.getJSONObject(j);

						FareDetailsModel faredetailsmodel = new FareDetailsModel();
						ArrayList<FareDetailsModel> faredetailsList = new ArrayList<FareDetailsModel>();
						faredetailsmodel.setBusId(tripsObj.getString("id"));

						faredetailsmodel.setbaseFare(faredetailsObj
								.getString("baseFare"));
						faredetailsmodel.setserviceTaxAbsolute(faredetailsObj
								.getString("serviceTaxAbsolute"));
						faredetailsmodel.setserviceTaxPercentage(faredetailsObj
								.getString("serviceTaxPercentage"));
						faredetailsmodel.settotalFare(faredetailsObj
								.getString("totalFare"));

						faredetailsList.add(faredetailsmodel);
						System.out
								.println("fare details has been set for bus id"
										+ tripModel.getBus_id());
						tripModel.setFaredetailsList(faredetailsList);
						System.out
								.println("------------>>>>>Fare details size:"
										+ tripModel.getFaredetailsList().size());

					}
				} else if (tripsObj.optJSONObject("fareDetails") instanceof JSONObject) {
					JSONObject faredetailsObj = tripsObj
							.getJSONObject("fareDetails");

					FareDetailsModel faredetailsmodel = new FareDetailsModel();
					ArrayList<FareDetailsModel> faredetailsList = new ArrayList<FareDetailsModel>();

					faredetailsmodel.setBusId(tripsObj.getString("id"));
					faredetailsmodel.setbaseFare(faredetailsObj
							.getString("baseFare"));
					faredetailsmodel.setserviceTaxAbsolute(faredetailsObj
							.getString("serviceTaxAbsolute"));
					faredetailsmodel.setserviceTaxPercentage(faredetailsObj
							.getString("serviceTaxPercentage"));
					faredetailsmodel.settotalFare(faredetailsObj
							.getString("totalFare"));

					faredetailsList.add(faredetailsmodel);
					System.out.println("fareDetails has been set for bus id"
							+ tripModel.getBus_id());
					tripModel.setFaredetailsList(faredetailsList);

				}

				// Prarsing fare

				if (tripsObj.optJSONArray("fares") instanceof JSONArray) {
					JSONArray fares = tripsObj.getJSONArray("fares");
					for (int l = 0; l < fares.length(); l++) {

						// JSONObject fareObj = fares.getJSONObject(l);
						FareModel fareModel = new FareModel();
						fareModel.setBusId(tripsObj.getString("id"));
						fareModel.setfare(fares.getString(l));
						// dbManager.insertfareInfo(Busid, fare);
						fareList.add(fareModel);
						tripModel.setFareList(fareList);
					}
				} else if (tripsObj.has("fares")) {

					FareModel fareModel = new FareModel();

					fareModel.setBusId(tripsObj.getString("id"));
					fareModel.setfare(tripsObj.getString("fares"));
					// dbManager.insertfareInfo(Busid, fare);
					fareList.add(fareModel);
					tripModel.setFareList(fareList);
				}

				tripModel.setidProofRequired(tripsObj
						.getString("idProofRequired"));
				tripModel.setliveTrackingAvailable(tripsObj
						.getString("liveTrackingAvailable"));
				tripModel.setnonAC(tripsObj.getString("nonAC"));
				tripModel.setoperator(tripsObj.getString("operator"));
				tripModel.setpartialCancellationAllowed(tripsObj
						.getString("partialCancellationAllowed"));
				tripModel.setrouteId(tripsObj.getString("routeId"));
				tripModel.setseater(tripsObj.getString("seater"));
				tripModel.setsleeper(tripsObj.getString("sleeper"));
				tripModel.settravels(tripsObj.getString("travels"));
				tripModel.setmTicketEnabled(tripsObj
						.getString("mTicketEnabled"));

				// dbManager.insertAllTrip(tripModel);
				busList.add(tripModel);

			}

			// dbManager.close();
			System.out.println("Returning with total bus of " + busList.size());
			return busList;
		} catch (Exception e) {
			e.printStackTrace();
			return busList;
		}

	}

	public static String ConvertTime(Long Time) {
		int quotient, hour, min, day;
		String actultime;
		quotient = (int) (Time / 60);
		min = (int) (Time % 60);
		hour = quotient % 24;
		day = (int) (quotient / 24);
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, day);
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.set(Calendar.MINUTE, min);
		// Calendar.HOUR_OF_DAY is in 24-hour format
		Date mDate = new Date(c.getTimeInMillis());
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
		actultime = dateFormat.format(mDate);
		return actultime;

	}

}
